package com.wonderful.eyepetizer.ui.pages.detail.viewmodel

import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import com.wonderful.eyepetizer.app.base.BaseViewModel
import com.wonderful.eyepetizer.data.repo.MainRepo
import com.wonderful.eyepetizer.ui.pages.detail.CategoryDetailActivity
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/22 15:18
 */
@HiltViewModel
class CategoryDetailViewModel @Inject constructor(
    val mRepo: MainRepo,
    savedStateHandle: SavedStateHandle
) : BaseViewModel() {
    var refreshing = mutableStateOf(false)

    // 通过 SavedStateHandle 来获取 activity/fragment/compose 中传递的参数
    // compose 所在的 activity 中的 intent.extras 包含有所需参数的话，用 saveStateHandle.get<T>() 来获取
    // androidX 会自动把 arguments 的所有参数存到 savedStateHandle
    private val categoryId = savedStateHandle.get<Int>(CategoryDetailActivity.KEY_CATEGORY_ID) ?: 0
    val categoryPageFlow =
        mRepo.getCategoryDetailList(categoryId, refreshing).cachedIn(viewModelScope)
}